﻿using System.Collections.Generic;
using System.Xml;

namespace ActiveStory.IO
{
    public class XmlMapFileWriter<TKey,TValue> : IMapFileWriter<TKey,TValue>
    {
        #region IMapFileWriter<TKey,TValue> Members

        public void SaveFile(string filename, IDictionary<TKey, TValue> map, string mainElementName, string mapItemElementName, string keyElementName, string valueElementName)
        {
            XmlWriterSettings xws = new XmlWriterSettings();
            xws.Indent = true;
            xws.CloseOutput = true;
            XmlWriter xw = XmlWriter.Create(filename, xws);
            xw.WriteStartDocument();
            xw.WriteStartElement(mainElementName);
            foreach (KeyValuePair<TKey, TValue> currPair in map)
            {
                xw.WriteStartElement(mapItemElementName);
                xw.WriteStartElement(keyElementName);
                xw.WriteValue(currPair.Key);
                xw.WriteEndElement();
                xw.WriteStartElement(valueElementName);
                xw.WriteValue(currPair.Value);
                xw.WriteEndElement();
                xw.WriteEndElement();
            }
            xw.WriteEndElement();
            xw.WriteEndDocument();
            xw.Flush();
            xw.Close();
        }

        #endregion
    }
}
